package cn.deschen.search.mysql.handlers;

import cn.deschen.biz.service.*;
import cn.deschen.search.constants.OpType;
import cn.deschen.search.index.IndexHandler;
import cn.deschen.search.index.adCreative.AdCreativeObject;
import cn.deschen.search.index.adKeyword.AdKeywordObject;
import cn.deschen.search.index.adPlan.AdPlanObject;
import cn.deschen.search.index.adRegion.AdRegionObject;
import cn.deschen.search.index.adTag.AdTagObject;
import cn.deschen.search.index.adUnit.AdUnitObject;
import cn.deschen.search.index.adUnitEle.AdUnitEleObject;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Author hanbin_chen
 * @Description 全量索引处理器
 * @DateTime 2021/6/17
 * @Version V1.0.0
 */
@Slf4j
@Component
public class FullIndexHandler {

    @Autowired
    private AdPlanService planService;

    @Autowired
    private AdCreativeService creativeService;

    @Autowired
    private AdTagService tagService;

    @Autowired
    private AdRegionService regionService;

    @Autowired
    private AdKeywordService keywordService;

    @Autowired
    private AdUnitService unitService;

    @Autowired
    private AdUnitEleService unitEleService;

    @Autowired
    private MapperFacade mapperFacade;

    @PostConstruct
    public void init() {
        List<AdPlanObject> planObjectList = mapperFacade.mapAsList(planService.list(), AdPlanObject.class);
        for (AdPlanObject planObject : planObjectList) {
            IndexHandler.handlerLevel2Index(planObject, OpType.ADD);
        }
        List<AdCreativeObject> creativeObjectList = mapperFacade.mapAsList(creativeService.list(), AdCreativeObject.class);
        for (AdCreativeObject creativeObject : creativeObjectList) {
            IndexHandler.handlerLevel2Index(creativeObject, OpType.ADD);
        }
        List<AdTagObject> tagObjectList = mapperFacade.mapAsList(tagService.list(), AdTagObject.class);
        for (AdTagObject tagObject : tagObjectList) {
            IndexHandler.handlerLevel2Index(tagObject, OpType.ADD);
        }
        List<AdRegionObject> regionObjectList = mapperFacade.mapAsList(regionService.list(), AdRegionObject.class);
        for (AdRegionObject regionObject : regionObjectList) {
            IndexHandler.handlerLevel2Index(regionObject, OpType.ADD);
        }
        List<AdKeywordObject> keywordObjectList = mapperFacade.mapAsList(keywordService.list(), AdKeywordObject.class);
        for (AdKeywordObject keywordObject : keywordObjectList) {
            IndexHandler.handlerLevel2Index(keywordObject, OpType.ADD);
        }
        List<AdUnitObject> unitObjectList = mapperFacade.mapAsList(unitService.list(), AdUnitObject.class);
        for (AdUnitObject unitObject : unitObjectList) {
            IndexHandler.handlerLevel3Index(unitObject, OpType.ADD);
        }
        List<AdUnitEleObject> unitEleObjectList = mapperFacade.mapAsList(unitEleService.list(), AdUnitEleObject.class);
        for (AdUnitEleObject adUnitEleObject : unitEleObjectList) {
            IndexHandler.handlerLevel4Index(adUnitEleObject, OpType.ADD);
        }

    }
}
